import axios from 'axios';

const api = axios.create({
  baseURL: import.meta.env.MODE === 'development' ? '/apDev' : '/api',
  timeout: 30000,
  withCredentials: true,
  headers: {}
});
const getImage = axios.create({
  baseURL: import.meta.env.MODE === 'development' ? '/imaDev' : '/imag',
  timeout: 300000,
  withCredentials: true,
  headers: {}
});

export { api, getImage };

export const getStreamTextSample = async (url: string): Promise<string> => {
  const response = await fetch(url);

  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`);
  }

  const reader = response.body?.getReader();
  if (!reader) {
    throw new Error('No reader available');
  }

  const decoder = new TextDecoder();
  let result = '';

  try {
    while (true) {
      const { done, value } = await reader.read();

      if (done) break;

      result += decoder.decode(value, { stream: true });
    }
  } finally {
    reader.releaseLock();
  }

  return result;
};
